package ua.cs.stu.ics.GUI;

import ua.cs.stu.ics.dao.dbf.MarkNewDAO;
import ua.cs.stu.ics.dao.dbf.StudentDAO;
import ua.cs.stu.ics.dbf.domain.MarkNewDomain;
import ua.cs.stu.ics.dbf.domain.StudentDomain;
import ua.cs.stu.ics.label.JLabelExtended;
import ua.cs.stu.ics.utils.Util;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * author: Admin
  */
public class SecondBlankView {

    private JPanel jPanel;
    private MarkNewDAO markNewDAO;
    private StudentDAO studentDAO;

    private int currentRow = 0;
    private int currentColumn = 0;
    private Double currentY = new Double(0);

    private static final int CELL_HEIGHT = 13;
    private static final Double STEP = new Double (13.3);

    private static final int MARK_WIDTH = 75;
    private static final int TIME_WIDTH = 40;
    private static final int SUBJECT_WIDTH = 165;

    private static final int[] MARGIN_TOP = new int[]{88, 60, 60};
    private static final int[] X_SUBJECT_COLUMN = new int[]{42, 297, 557};
    private static final int[] X_TIME_COLUMN = new int[]{165, 435, 695};
    private static final int[] X_MARK_COLUMN = new int[]{210, 475, 740};
    private static final int[] ROW_COUNT = new int[]{34, 36, 36};

    private static final int SUBJECT_MAX_LENGTH = 33;

    private int k = 0;


//    private static final int FIRST_COLUMN_ROW_COUNT


    public void setjPanel(JPanel jPanel) {
        this.jPanel = jPanel;
    }

    public SecondBlankView(JPanel jPanel) {
        this.jPanel = jPanel;
    }

    public SecondBlankView() {
    }

    public void view(StudentDomain studentDomain) {
        currentRow = 0;
        currentColumn = 0;
        currentY = new Double(0);
        jPanel.removeAll();
        viewSection((ArrayList<MarkNewDomain>) markNewDAO.getSection("e", studentDomain), studentDomain);
        viewSection((ArrayList<MarkNewDomain>) markNewDAO.getSection("z", studentDomain), studentDomain);
        writeTittle("Курсові проекти і роботи", true, true);
        viewSection((ArrayList<MarkNewDomain>) markNewDAO.getSection("k", studentDomain), studentDomain);
        writeTittle("Практики", false, true);
        viewSection((ArrayList<MarkNewDomain>) markNewDAO.getSection("п", studentDomain), studentDomain);
        writeTittle("", true, false);

        int time = markNewDAO.getSumTime(studentDomain);
        JLabelExtended jlabel = new JLabelExtended(time + "");
        jlabel.setName("labeltime2" + currentRow + "ee" + currentY);
        jlabel.setBounds(new Rectangle(X_TIME_COLUMN[2] - 10, 528, MARK_WIDTH, CELL_HEIGHT));
        jlabel.setFont(new Font("Dialog", Font.PLAIN, 8));
        jlabel.setVerticalAlignment(SwingConstants.BOTTOM);
        jlabel.setHorizontalAlignment(SwingConstants.CENTER);
        jlabel.setEnabled(true);
        jlabel.setVisible(true);
        getjPanel().add(jlabel);
    }


    public JPanel getjPanel() {
        if (jPanel == null)
            throw new NullPointerException(jPanel.getName());
        return jPanel;
    }


    private void viewSection(ArrayList<MarkNewDomain> list, StudentDomain studentDomain) {
        int j = 0;
        for (MarkNewDomain mark : list) {
            if (currentRow == 0)
                currentY = new Double(MARGIN_TOP[currentColumn]);
            String markStr = null;
            if (mark.getMark().equals("3"))
                markStr = "задовільно";
            if (mark.getMark().equals("4"))
                markStr = "добре";
            if (mark.getMark().equals("5"))
                markStr = "відмінно";

            ArrayList<String> subjects = Util.trimToArray(mark.getNamedis().replaceAll("[ ]+$", ""), SUBJECT_MAX_LENGTH);

            for (int i = 0; i < subjects.size(); i++) {
                String subject = subjects.get(i);
                JLabelExtended jLabelSubject = new JLabelExtended(subject);
                jLabelSubject.setName("labelsubject" + currentRow + "aa" + currentColumn);
                jLabelSubject.setBounds(new Rectangle(X_SUBJECT_COLUMN[currentColumn], currentY.intValue(), SUBJECT_WIDTH, CELL_HEIGHT));
                jLabelSubject.setFont(new Font("Dialog", Font.PLAIN, 8));
                jLabelSubject.setVerticalAlignment(SwingConstants.BOTTOM);
                jLabelSubject.setHorizontalAlignment(SwingConstants.LEFT);
                jLabelSubject.setSize(SUBJECT_WIDTH - 20, CELL_HEIGHT);
                jLabelSubject.setEnabled(true);
                jLabelSubject.setVisible(true);
                getjPanel().add(jLabelSubject);
                if (i != subjects.size() - 1) {
                    currentY += STEP;
                    currentRow++;
                    if (currentRow == ROW_COUNT[currentColumn]) {
                        currentRow = 0;
                        currentColumn++;
                        currentY = new Double(MARGIN_TOP[currentColumn]);
                    }
                }
            }

            JLabelExtended jlabelMark = new JLabelExtended(markStr);
            JLabelExtended jLabelTime = new JLabelExtended(mark.getHours().intValue() + "");

            jlabelMark.setName("labelmark" + currentRow + "bb" + currentColumn);
            jLabelTime.setName("labeltime" + currentRow + "cc" + currentColumn);

            jlabelMark.setBounds(new Rectangle(X_MARK_COLUMN[currentColumn], currentY.intValue(), MARK_WIDTH, CELL_HEIGHT));
            jLabelTime.setBounds(new Rectangle(X_TIME_COLUMN[currentColumn], currentY.intValue(), TIME_WIDTH, CELL_HEIGHT));

            jlabelMark.setFont(new Font("Dialog", Font.PLAIN, 8));
            jLabelTime.setFont(new Font("Dialog", Font.PLAIN, 8));

            jlabelMark.setVerticalAlignment(SwingConstants.BOTTOM);
            jLabelTime.setVerticalAlignment(SwingConstants.BOTTOM);

            jlabelMark.setHorizontalAlignment(SwingConstants.CENTER);
            jLabelTime.setHorizontalAlignment(SwingConstants.CENTER);

            jlabelMark.setEnabled(true);
            jLabelTime.setEnabled(true);

            jlabelMark.setVisible(true);
            jLabelTime.setVisible(true);

            getjPanel().add(jlabelMark);
            getjPanel().add(jLabelTime);

            currentY += STEP;
            currentRow++;
            if (currentRow == ROW_COUNT[currentColumn]) {
                currentRow = 0;
                currentColumn++;
            }
        }


    }

    private void writeTittle(String tittle, boolean dotted, boolean tittled) {
        if (dotted) {
            JLabelExtended jLabelSubject = new JLabelExtended("...................");
            jLabelSubject.setName("labelsubject" + currentRow + "oo" + currentColumn);
            jLabelSubject.setBounds(new Rectangle(X_SUBJECT_COLUMN[currentColumn], currentY.intValue(), SUBJECT_WIDTH, CELL_HEIGHT));
            jLabelSubject.setFont(new Font("Dialog", Font.PLAIN, 8));
            jLabelSubject.setVerticalAlignment(SwingConstants.BOTTOM);
            jLabelSubject.setHorizontalAlignment(SwingConstants.CENTER);
            jLabelSubject.setEnabled(true);
            jLabelSubject.setVisible(true);
            getjPanel().add(jLabelSubject);

            JLabelExtended jlabelMark = new JLabelExtended(".......");
            JLabelExtended jLabelTime = new JLabelExtended(".......");

            jlabelMark.setName("labelmark" + currentRow + "zz" + currentColumn);
            jLabelTime.setName("labeltime" + currentRow + "xx" + currentColumn);

            jlabelMark.setBounds(new Rectangle(X_MARK_COLUMN[currentColumn], currentY.intValue(), MARK_WIDTH, CELL_HEIGHT));
            jLabelTime.setBounds(new Rectangle(X_TIME_COLUMN[currentColumn], currentY.intValue(), TIME_WIDTH, CELL_HEIGHT));

            jlabelMark.setFont(new Font("Dialog", Font.PLAIN, 8));
            jLabelTime.setFont(new Font("Dialog", Font.PLAIN, 8));

            jlabelMark.setVerticalAlignment(SwingConstants.BOTTOM);
            jLabelTime.setVerticalAlignment(SwingConstants.BOTTOM);

            jlabelMark.setHorizontalAlignment(SwingConstants.CENTER);
            jLabelTime.setHorizontalAlignment(SwingConstants.CENTER);

            jlabelMark.setEnabled(true);
            jLabelTime.setEnabled(true);

            jlabelMark.setVisible(true);
            jLabelTime.setVisible(true);

            getjPanel().add(jlabelMark);
            getjPanel().add(jLabelTime);

            currentY += STEP;
            currentRow++;
            if (currentRow == ROW_COUNT[currentColumn]) {
                currentRow = 0;
                currentColumn++;
                currentY = new Double(MARGIN_TOP[currentColumn]);
            }
        }
        if (tittled) {
            JLabelExtended jLabelSubject = new JLabelExtended(tittle);
            jLabelSubject.setName("labelsubject" + currentRow + "pppp" + currentColumn);
            jLabelSubject.setBounds(new Rectangle(X_SUBJECT_COLUMN[currentColumn], currentY.intValue(), SUBJECT_WIDTH, CELL_HEIGHT));
            jLabelSubject.setFont(new Font("Dialog", Font.ITALIC, 8));
            jLabelSubject.setVerticalAlignment(SwingConstants.BOTTOM);
            jLabelSubject.setHorizontalAlignment(SwingConstants.LEFT);
            jLabelSubject.setEnabled(true);
            jLabelSubject.setVisible(true);
            getjPanel().add(jLabelSubject);
            currentY += STEP;
            currentRow++;
            if (currentRow == ROW_COUNT[currentColumn]) {
                currentRow = 0;
                currentColumn++;
                currentY = new Double(MARGIN_TOP[currentColumn]);
            }
        }
    }

    public MarkNewDAO getMarkNewDAO() {
        return markNewDAO;
    }

    public void setMarkNewDAO(MarkNewDAO markNewDAO) {
        this.markNewDAO = markNewDAO;
    }

    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    //    private void viewMark()
}
